package com.hibase.upms.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdNullEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.upms.constant.MenuManagerConstant;
import com.hibase.upms.entity.menu.MdsMenu;
import com.hibase.upms.entity.menu.vo.*;
import com.hibase.upms.service.menu.MdsMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ChrisCui
 * @since 2019-04-18
 */
@RestController
@RequestMapping("/mds-menu")
@Api(tags = "菜单管理")
@RequiredArgsConstructor
public class MdsMenuController {

    private final MdsMenuService menuService;

    @ApiOperation(value = "加载首页模块菜单列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectList")
    public ResponseModel<List<MdsMenuIndexVO>> selectList() {
        return ResponseModel.successMsg(EntityUtil.transformByJson(menuService.selectList(), MdsMenuIndexVO.class));
    }

    /**
     * 返回树形菜单集合
     * @return 树形菜单
     */
    @HibasePostRequestMapping(value = "tree")
    public ResponseModel getTree(@RequestBody MdsMenuTreeVO treeVO) {
        return ResponseModel.successMsg(menuService.treeMenu(treeVO));
    }

    /**
     * 加载PDA模块菜单列表
     * <p>
     * Deprecated 2022年3月2日 建议使用 selectPdaMenu
     *
     * @return
     */
    @Deprecated
    @ApiOperation(value = "加载PDA模块菜单列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectPDAList")
    public ResponseModel<List<MdsMenuIndexVO>> selectPDAList() {
        List<String> result = Lists.newArrayList();
        List<MdsMenuIndexVO> mdsMenuIndexVOS = EntityUtil.transformByJson(menuService.selectPDAList(), MdsMenuIndexVO.class);
        if (CollUtil.isNotEmpty(mdsMenuIndexVOS)) {
            result = mdsMenuIndexVOS.get(0).getMenuBODetail().stream().map(MdsMenuIndexVO::getCode).collect(Collectors.toList());
        }

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "[新]加载PDA模块菜单列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectPdaMenu")
    public ResponseModel<List<MdsMenuIndexVO>> selectPdaMenu() {
        List<MdsMenuIndexVO> padMenus = EntityUtil.transformByJson(menuService.selectPDAList(), MdsMenuIndexVO.class);
        return ResponseModel.successMsg(padMenus);
    }

    @ApiOperation(value = "新建菜单", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/addMenuInfo")
    public ResponseModel insertMenu(@RequestBody MdsMenuInsertVO menuInsertVO) {
        MdsMenuUpdateVO updateVO = EntityUtil.transform(menuInsertVO, MdsMenuUpdateVO.class);
        menuService.changeMenu(updateVO);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "更新菜单", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/updateMenuInfo")
    public ResponseModel updateMenu(@RequestBody MdsMenuUpdateVO menuUpdateVO) {
        menuService.changeMenu(menuUpdateVO);
        //此处需要返回对象信息
        return ResponseModel.successMsg(menuUpdateVO.getId());
    }

    @ApiOperation(value = "启用菜单", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/enableMenuInfo")
    public ResponseModel enableMenu(@RequestBody OperationIdsEntity menuIdsVo) {
        menuService.changeMenuStatus(menuIdsVo.getIds(), MenuManagerConstant.RECORD_STATUS_ENABLE);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "停用菜单", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/disabledMenuInfo")
    public ResponseModel disabledMenu(@RequestBody OperationIdsEntity menuIdsVo) {
        menuService.changeMenuStatus(menuIdsVo.getIds(), MenuManagerConstant.RECORD_STATUS_DISABLED);
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "查询菜单列表（非层级）", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectMenuList")
    public ResponseModel<List<MdsMenuIndexVO>> selectMenuList(@RequestBody MdsMenuSearchVO searchVO) {
        PageInfo result = menuService.selectMenuList(searchVO);
        result.setList(EntityUtil.transform(result.getList(), MdsMenuVO.class));
        return ResponseModel.successMsg(result);
    }


    @ApiOperation(value = "查询菜单详情", httpMethod = "POST", response = MdsMenuViewVO.class)
    @HibasePostRequestMapping(value = "viewDetails")
    public ResponseModel viewDetails(@RequestBody OperationIdEntity idEntity) {
        MdsMenu mdsMenu = menuService.getById(idEntity.getId());
        MdsMenuViewVO mdsMenuViewVo = EntityUtil.transform(mdsMenu, MdsMenuViewVO.class);
        return ResponseModel.successMsg(mdsMenuViewVo);
    }


    @ApiOperation(value = "加载菜单列表（层级）", httpMethod = "POST", response = MdsMenuVO.class)
    @HibasePostRequestMapping(value = "selectAllMenuList")
    public ResponseModel selectAllMenuList() {
        // TODO, 如果放开，用mds_menu 的数据 ，返回的id 需要时mds_menu 的code.
        return ResponseModel.successMsg(EntityUtil.transformByJson(menuService.selectAllMenuList(), MdsMenuIndexVO.class));
    }

    @ApiOperation(value = "加载分组菜单列表（层级）", httpMethod = "POST", response = MdsMenuVO.class)
    @HibasePostRequestMapping(value = "selectAllMenuGroupList")
    public ResponseModel selectAllMenuGroupList() {
        return ResponseModel.successMsg(menuService.selectAllMenuGroupList());
    }

    @ApiOperation(value = "根据菜单层级，第一级id传入为空", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectFirstMenu")
    public ResponseModel<MdsMenuVO> selectFirstMenu(@RequestBody OperationIdNullEntity idEntity) {

        QueryWrapper<MdsMenu> wrapper = new QueryWrapper<>();

        if (StrUtil.isBlank(idEntity.getId())) {

            wrapper.isNull("parent_id");
        } else {
            wrapper.eq("parent_id", idEntity.getId());
        }
        wrapper.eq("be_active", MenuManagerConstant.RECORD_STATUS_ENABLE);
        List<MdsMenu> result = menuService.list(wrapper);

        return ResponseModel.successMsg(EntityUtil.transform(result, MdsMenuVO.class));
    }


    @ApiOperation(value = "importMenu", httpMethod = "POST")
    @HibasePostRequestMapping(value = "importMenu")
    public ResponseModel importMenu(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        try {
            HibaseAssert.notNull(multipartFile.getInputStream(), "请上传文件");
        } catch (Exception e) {
            return ResponseModel.failMsg(2, "上传文件失败");
        }
        menuService.importMenu(multipartFile);
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "新增所有菜单数据", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/addAll")
    public ResponseModel addAll(@RequestBody OperationIdEntity operationIdEntity) {
        menuService.addAll(operationIdEntity.getId());
        //此处需要返回对象信息
        return ResponseModel.successMsg();
    }
}